package com.shirley.pers.zhufengfmvi.customViews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;

import com.shirley.pers.zhufengfmvi.R;

/**
 * Description: 自定义RadioButton.用来适配图片资源<br/>
 * <p/>
 * Copyright (c) , 2016, Shirley Xian <br/>
 * This program is protected by copyright laws. <br/>
 * ClassName: CustomTabRadioButton<br/>
 * Date: 2016/4/5 22:33
 *
 * @author Shirley Xian
 * @version 1.0
 */
public class CustomTabRadioButton extends RadioButton {
    private static final String TAG = CustomTabRadioButton.class.getSimpleName();
    private int mDrawableSize;

    public CustomTabRadioButton(Context context) {
        this(context, null);
    }

    public CustomTabRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTabRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTabRadioButton);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            Log.i(TAG, "CustomTabRadioButton: attr:" + attr);
            switch (attr) {
                case R.styleable.CustomTabRadioButton_custom_drawableSize:
                    mDrawableSize = typedArray.getDimensionPixelSize(R.styleable.CustomTabRadioButton_custom_drawableSize, 50);
                    break;
                case R.styleable.CustomTabRadioButton_custom_drawableTop:
                    drawableTop = typedArray.getDrawable(attr);
                    break;
                case R.styleable.CustomTabRadioButton_custom_drawableLeft:
                    drawableLeft = typedArray.getDrawable(attr);
                    break;
                case R.styleable.CustomTabRadioButton_custom_drawableBottom:
                    drawableBottom = typedArray.getDrawable(attr);
                    break;
                case R.styleable.CustomTabRadioButton_custom_drawableRight:
                    drawableRight = typedArray.getDrawable(attr);
                    break;
            }
        }
        typedArray.recycle();
        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        setCompoundDrawables(left, top, right, bottom);
    }
}
